<?php
namespace My\Controller;

use Utuo\BackgroundController;

class ModuleSlideController extends BackgroundController {
	
	public function _initialize() {
		$this->setControllerTitle("轮播图管理");
		$this->setActionTitle(array(
				'index' => '轮播图设置',
		));
	}
	
	// 轮播图设置
	public function indexAction() {
		$m = M("ModuleSlide");
		if(!$m->where("id=%d", $this->aid)->count()){
			if(!$m->add(array('id' => $this->aid, 'data' => serialize(array())))){
				$this->error("添加轮播图数据失败");
			}
		}
		$moduleSlide = $m->find($this->aid);
		$data = unserialize($moduleSlide['data']);
		$moduleSlide['data'] = $data;
		$moduleSlide['dataJSON'] = json_encode($data);
		$this->assign("moduleSlide", $moduleSlide);
		$this->display();
	}
	
	// 提交轮播图设置
	public function indexPostAction() {
		$d = M('ModuleSlide');
		if ($d->create()) {
			$d->id = $this->aid;
			if(empty($d->data)) $data = array(); else $data = explode(",", $d->data);
			$data = array_filter($data, function($v){if(empty($v)) return false; return true;});
			/*轮播配置信息*/
			$config = array();
			foreach($data as $v){
				$cfg_item = explode('|',$v);
				/*配置项*/
				$item = array(
					'img' => $cfg_item[2],
					'url' => $cfg_item[1],
					'title' => $cfg_item[0],
				);
				$config[] = $item;
			}
			$d->data = serialize($config);
			if (false !== $d->save()) {
				$this->ajaxSuccess('保存轮播图成功');
			}
		} 
		$this->ajaxError($d->getError());
	}
	
	// 文件上传
	public function uploadAction() {
		$upload = new \Utuo\Upload();
		$info = $upload->uploadOne();
		if (!$info) {
			$this->ajaxError($upload->getError());
		}else{
			$this->ajaxSuccess("上传成功", $info);
		}
	}
	
	
}